package com.example.littleken.secret.Activitys;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.littleken.secret.Config;
import com.example.littleken.secret.Net.Publish;
import com.example.littleken.secret.R;

/**
 * 本类实现了发布消息页面的具体逻辑
 */
public class AtyPublish extends AppCompatActivity {

    private EditText etMessageContent;
    private String phone_md5;
    private String token;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aty_pub_message);
        etMessageContent = (EditText) findViewById(R.id.et_MessageContent);

        //数据初始化
        Intent intent = getIntent();
        phone_md5 = intent.getStringExtra(Config.KEY_PHONE_MD5);
        token = intent.getStringExtra(Config.KEY_TOKEN);
        //实现发布消息功能
        findViewById(R.id.send_publish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(etMessageContent.getText().toString())){
                    Toast.makeText(AtyPublish.this,R.string.publish_can_not_be_null,Toast.LENGTH_SHORT).show();
                    return;
                }
                final ProgressDialog pd = ProgressDialog.show(AtyPublish.this,getResources().getString(R.string.connecting),getResources().getString(R.string.connecting_to_server));
                new Publish(phone_md5, token, etMessageContent.getText().toString(), new Publish.SuccessCallback() {
                    @Override
                    public void onSuccess() {
                        pd.dismiss();
                        setResult(Config.ACTIVITY_NEED_RESULT_NEED_REFRESH);//通知AtyTimeline刷新列表
                        Toast.makeText(AtyPublish.this,R.string.success_to_send_publish,Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }, new Publish.FailCallback() {
                    @Override
                    public void onFail(int errorCode) {
                        pd.dismiss();
                        if(errorCode == Config.RESULT_STATUS_INVALID_TOKEN){
                            startActivity(new Intent(AtyPublish.this,AtyLogin.class));
                            finish();
                        }else{
                            Toast.makeText(AtyPublish.this,R.string.fail_to_send_publish,Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}
